a='Hello, python world!'
a_len=len(a)
a_o=a.count('o')
a_upper=a.upper()
a_lower=a.lower()
print(a_len,a_o)
print(a_lower, "\n",a_upper)
py_position=a.find('python')
print(py_position)
a_re=a.replace('world','universe')
print(a_re)
a_point=a.startswith('Hello')
print(a_point)
a_tail=a.endswith('!')
print(a_tail)
a_split=a.split(',')
print(a_split)
a_strip=a.rstrip("!")
print(a_strip)
b='hello,'
c='world!'
d=b+c
print(d)#法一
e=(' ').join((b,c))
print(e)
a_reverse= ''.join(reversed(a)) #字符串反转
print(a_reverse)
a_revse=a[::-1]
print(a_revse)
f=a[6:-7]#切片python
print(f)
a_del=a.strip('Hello，')
print(a_del)#hello删除
#a_co=f-string(a,'2021')使用f-string合并字符串和数字2021
a_a=a.find('a')
print(a_a)
a_e=a.rfind('o')#字符‘o’最后一次出现的位置
print(a_e)
a_bl=a.count(' ')
print(a_bl)#空格的次数
for char in a:
    print(char)#遍历字符
    i=0
  #法2 while i<len(a):
        #print(a[i]) i+=1

#s = "hello"
c#har_list = list(s)
#print(char_list)
#a_list=list(a)
#print(a_list)法1
char_alist=[char for char in a]
print(char_alist)
char_alist.sort()#排序
print(char_alist)
print(a.isdigit())#是否为数字
words = ['Hello,', 'Python', 'World!']
result = ' '.join(words)
print(result)
number='100'
s='number:%s'%(number)
print(s)
print(a.islower())#检查字符串是否全部为小写字母
print(a.isupper())#检查字符串是否全部为大写字母
n_list=[1,2,3]
n_list='_'.join(map(str,n_list))
print(n_list)
s = "hello world"
positions = []
index = s.find('o')
while index != -1:
    positions.append(index)
    index = s.find('o', index + 1)
print(positions)#出字符串中所有的"o"的位置
print(a.replace('o','O'))#替换字符串中的第一个"o"为"O"
original_string = "Hello World"
insert_point = 6
substring = "amazing "

# 插入点之前的字符串
before_insert = original_string[:insert_point]
# 插入点之后的字符串
after_insert = original_string[insert_point:]

# 拼接
new_string = before_insert + substring + after_insert
print(new_string)

#进阶
s='python is great!'
s_reversed=s[::-1]
print(s_reversed)
print(s.count('t'))
s_replace=s.replace(' ','_')
print(s_replace)
s2 = "A man, a plan, a canal, Panama!"

# 移除标点并转换为小写
cleaned_s2 = ''.join(char.lower() for char in s2 if char.isalnum())

# 检查是否是回文
is_palindrome = cleaned_s2 == cleaned_s2[::-1]
print(is_palindrome)  # 输出：True
s_is=s.find('is')
print(s_is)
s_t=s.find('t')
s_inserted='_inserted'
s_sult=s[:s_t+1]+s_inserted+s[s_t+1:]
print(s_sult)
s_aw=s.replace('great','awesome')
print(s_aw)#替换
#切片一半，反顺序连接
s_len=len(s)
s_partone=s[:s_len//2:-1]
s_parttwo=s[s_len//2::-1]
s_pp=s_partone+s_parttwo
print(s_pp)
print(s_partone)
# 使用split()方法分割字符串为单词列表
words = s.split()
# 使用列表生成式提取每个单词的首字母
first_letters = [word[0] for word in words]

print(first_letters)


def encrypt(text, mapping):
    # 创建一个映射字典的副本，以防在加密过程中被修改
    encrypted_text = ""
    for char in text:
        if char.isalpha():  # 确保只处理字母
            if char.islower():  # 处理小写字母
                encrypted_text += mapping[char].lower()
            else:  # 处理大写字母
                encrypted_text += mapping[char].upper()
        else:
            encrypted_text += char  # 非字母字符保持不变
    return encrypted_text


# 定义映射字典
mapping = {
    'a': 'm', 'b': 'n', 'c': 'o', 'd': 'p', 'e': 'q', 'f': 'r', 'g': 's',
    'h': 't', 'i': 'u', 'j': 'v', 'k': 'w', 'l': 'x', 'm': 'y', 'n': 'z',
    'o': 'a', 'p': 'b', 'q': 'c', 'r': 'd', 's': 'e', 't': 'f', 'u': 'g',
    'v': 'h', 'w': 'i', 'x': 'j', 'y': 'k', 'z': 'l'
}

# 测试加密函数
original_text = "Hello, World!"
encrypted_text = encrypt(original_text, mapping)
print("Original:", original_text)
print("Encrypted:", encrypted_text)

